package com.bdsc.blogs.mirror.demo;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.*;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

/**
 * @author 马凯林
 */
@Component
public class IocDemo
    implements BeanNameAware,
    BeanFactoryAware,
    ApplicationContextAware,
    InitializingBean,
    DisposableBean {

  private static final Logger logger = LogManager.getLogger(IocDemo.class);

  @Override
  public void setBeanName(String s) {
    logger.info(
        "【" + this.getClass().getSimpleName() + "】" + "implements BeanNameAware setBeanName");
  }

  @Override
  public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
    logger.info(
        "【" + this.getClass().getSimpleName() + "】" + "implements BeanFactoryAware setBeanFactory");
  }

  @Override
  public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
    logger.info(
        "【"
            + this.getClass().getSimpleName()
            + "】"
            + "implements ApplicationContextAware setApplicationContext");
  }

  @PostConstruct
  public void init() {
    logger.info("【" + this.getClass().getSimpleName() + "】" + "@PostConstruct");
  }

  @Override
  public void afterPropertiesSet() throws Exception {
    logger.info(
        "【"
            + this.getClass().getSimpleName()
            + "】"
            + "implements InitializingBean afterPropertiesSet");
  }

  @PreDestroy
  public void preDestroy() {
    logger.info("【" + this.getClass().getSimpleName() + "】" + "@PreDestroy");
  }

  @Override
  public void destroy() throws Exception {
    logger.info("【" + this.getClass().getSimpleName() + "】" + "implements DisposableBean destroy");
  }
}
